package com.example.costumview.activity2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.example.costumview.R;
import com.example.costumview.customview2.ChartView;
import com.example.costumview.model.ChartDataResponseBean;
import com.example.costumview.utils.EncodeUtils;
import com.example.costumview.utils.GetJsonDataUtil;
import com.google.gson.Gson;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ChartViewActivity extends AppCompatActivity {

    RelativeLayout rlChartContent;
    private ChartView cv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chart_view);

        initView();
    }

    private void initView() {

        rlChartContent = findViewById(R.id.rl_chart_content);

//        cv = new ChartView(this);
//
//        rlChartContent.addView(cv, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);

        initData();

    }

    private void initData() {
        ChartDataResponseBean testBean=new Gson().fromJson(new GetJsonDataUtil().getJson(this,"chart.json"),ChartDataResponseBean.class);

        Log.e("TAG", "onNext==================== "+new GetJsonDataUtil().getJson(this,"chart.json"));
        Log.e("TAG", "initData的ECG数量:"+testBean.getECGData().size());
        Map<String, ArrayList<Integer>> map = new HashMap<>();
        for (int i = 0; i < testBean.getECGData().size(); i++) {
            byte[] bytes = EncodeUtils.base64Decode(testBean.getECGData().get(i));
            short[] shorts = bytesToShorts(bytes);
            ArrayList<Integer> daolianList = new ArrayList<>();
            for (short aShort : shorts) {
                daolianList.add((int) aShort);
            }
            map.put(testBean.getTaskInfo().getLeadList().get(i), daolianList);
        }
        calculateOtherDaolian(map, testBean);

    }

    /*III、aVR、aVL、aVF这四个导联是计算出来的，公式：
    III = II - I；
    aVR = (I + II) / -2;
    aVL = I - II / 2;
    aVF = II - I / 2;*/
    private void calculateOtherDaolian(Map<String, ArrayList<Integer>> map, ChartDataResponseBean responseData) {
        //计算III
        if (!map.containsKey("I") || !map.containsKey("II")) {
            //没有I和II导联,后边的计算也没法进行
            return;
        }
        ArrayList<Integer> list_II = map.get("II");
        ArrayList<Integer> list_I = map.get("I");
        ArrayList<Integer> list_III = new ArrayList<>();
        ArrayList<Integer> list_aVR = new ArrayList<>();
        ArrayList<Integer> list_aVL = new ArrayList<>();
        ArrayList<Integer> list_aVF = new ArrayList<>();
        for (int i = 0; i < list_II.size(); i++) {
            if (!map.containsKey("III")) {
                list_III.add(list_II.get(i) - list_I.get(i));
            }
            if (!map.containsKey("aVR")) {
                list_aVR.add((list_II.get(i) + list_I.get(i)) / -2);
            }
            if (!map.containsKey("aVL")) {
                list_aVL.add(list_I.get(i) - list_II.get(i) / 2);
            }
            if (!map.containsKey("aVF")) {
                list_aVF.add(list_II.get(i) - list_I.get(i) / 2);
            }


        }
        if (!map.containsKey("III")) {
            map.put("III", list_III);
        }
        if (!map.containsKey("aVR")) {
            map.put("aVR", list_aVR);
        }
        if (!map.containsKey("aVL")) {
            map.put("aVL", list_aVL);
        }
        if (!map.containsKey("aVF")) {
            map.put("aVF", list_aVF);
        }

        cv.setData(map, responseData);
    }

    public static short[] bytesToShorts(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        short[] shorts = new short[bytes.length / 2];
        ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
        return shorts;
    }
}